package com.greyshark.lib.bluetooth.core.internal

import android.Manifest
import android.app.Notification
import android.app.Service
import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import android.content.pm.PackageManager
import android.content.pm.ServiceInfo
import android.os.Build
import android.text.TextUtils
import androidx.core.content.ContextCompat

internal class BackgroundNotificationExecutor {

    private var notifyTittle: String = ""
    private var notifyText: String = ""
    private var notifyAppIcon: Int = 0

    companion object {
        val instance: BackgroundNotificationExecutor by lazy(
            mode = LazyThreadSafetyMode.SYNCHRONIZED
        ) {
            BackgroundNotificationExecutor()
        }
    }

    private var notification: Notification? = null
    private var notificationId = 0

    fun startForgeGround(service: Service) {
        try {
            if (!TextUtils.isEmpty(notifyTittle) && !TextUtils.isEmpty(notifyText)) {
                val notificationIntent = Intent(service, NotificationClickReceiver::class.java)
                notificationIntent.setAction(NotificationClickReceiver.CLICK_NOTIFICATION)
                val notification: Notification = NotificationUtils.createNotification(
                    service,
                    notifyTittle,
                    notifyText,
                    notifyAppIcon,
                    notificationIntent
                )
                if (service.applicationInfo.targetSdkVersion >= Build.VERSION_CODES.UPSIDE_DOWN_CAKE) {
                    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.UPSIDE_DOWN_CAKE) {
                        if (ContextCompat.checkSelfPermission(
                                service, Manifest.permission.BLUETOOTH_SCAN
                            ) == PackageManager.PERMISSION_GRANTED || ContextCompat.checkSelfPermission(
                                service, Manifest.permission.BLUETOOTH_CONNECT
                            ) == PackageManager.PERMISSION_GRANTED
                        ) {
                            service.startForeground(
                                javaClass.hashCode(),
                                notification,
                                ServiceInfo.FOREGROUND_SERVICE_TYPE_CONNECTED_DEVICE
                            )
                        }
                    } else {
                        service.startForeground(javaClass.hashCode(), notification)
                    }
                } else {
                    service.startForeground(javaClass.hashCode(), notification)
                }
                this.notification = notification
                notificationId = javaClass.hashCode()
            }
        } catch (e: Exception) {
            e.printStackTrace()
        }
    }

    fun getNotification(): Notification? {
        return notification
    }

    fun getNotificationId(): Int {
        return notificationId
    }

    fun removeNotification(service: Service) {
        if (!TextUtils.isEmpty(notifyTittle) && !TextUtils.isEmpty(notifyText)) {
            service.stopForeground(true)
        }
    }

    class NotificationClickReceiver : BroadcastReceiver() {
        override fun onReceive(context: Context?, intent: Intent) {
            try {
                if (intent.action == CLICK_NOTIFICATION) {
                    notificationClickCallBack?.onNotificationClick()
                }
            } catch (e: java.lang.Exception) {
                e.printStackTrace()
            }
        }

        companion object {
            const val CLICK_NOTIFICATION: String = "CLICK_NOTIFICATION"

            private var notificationClickCallBack: NotificationClickCallBack? = null

            fun setNotificationClickCallBack(notificationClickCallBack: NotificationClickCallBack?) {
                Companion.notificationClickCallBack = notificationClickCallBack
            }
        }
    }

    interface NotificationClickCallBack {
        fun onNotificationClick()
    }
}